<?php

function sendSMS($config,$tugas,$tgla,$tglb,$where){
	$ukey = $config['ukey'];
	$pkey = $config['pkey'];
	$phone = array();
	$db =  new db($config);
	$que = "SELECT no_hape FROM mn_crew WHERE 1";
	$nomors = $db->query($que);
	foreach($nomors as $nomor){
		if ($nomor['no_hape']!=''){
			$nmr = '0'.$nomor['no_hape'];
			array_push($phone, $nmr);
		}
	}
	$message = "[Agenda Idebeda]\n\"".$tugas."\"\n".$tgla." s.d ".$tglb."\nTempat : ".$where;
	$message = helper::trimMsg($message,110)."\n\nLihat idebeda.com/inti/agenda";
	foreach ($phone as $nomor) {
		$url = 'http://zenziva.com/apps/smsapi.php?userkey='.$ukey.'&passkey='.$pkey.'&nohp='.$nomor.'&pesan='.urlencode($message);
		//echo $message."\n\n";
		$curlHandle = curl_init();
		curl_setopt($curlHandle, CURLOPT_URL, $url);
		//curl_setopt($curlHandle, CURLOPT_HEADER, 0);
		curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curlHandle, CURLOPT_USERAGENT, 'Codular Sample cURL Request');
		curl_setopt($curlHandle, CURLOPT_TIMEOUT,30);
		$results = curl_exec($curlHandle);
		curl_close($curlHandle);
	}
	$_SESSION['sentsucc'] = "Pesan berhasil dikirim";
}

function addAgenda($config,$tugas,$ket,$tgla,$tglb){
	$db =  new db($config);
	$agenda ="INSERT INTO `mn_agenda` (`title` ,`start` ,`due` ,`ket` ,`by`) VALUES ('$tugas', '$tgla', '$tglb', '$ket', '".$_SESSION['logged']['id']."');";
	$db->exec($agenda);
}

function controller_agenda($config) {
    if (isset($_POST['kirim'])){
		Zend_Loader::loadClass('Zend_Gdata');
		Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
		Zend_Loader::loadClass('Zend_Gdata_Calendar');
		Zend_Loader::loadClass('Zend_Http_Client');
		//
		$gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
		$user = "ans4175@gmail.com"; 
		$pass = "ans4175muslim";
		$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);
		$gcal = new Zend_Gdata_Calendar($client);
		try {
			//var_dump($_POST);
			$event = $gcal->newEventEntry();	//bikin event baru
			$event->title = $gcal->newTitle($_POST['tugas']);	//judul
			$tugas = $_POST['tugas'];
			$ket = $_POST['ket'];
			$where = $_POST['tempat'];
			$event->where = array($gcal->newWhere($where.", Indonesia"));	//lokasi
			$event->content = $gcal->newContent($ket);	//keterangan
			//        
			$dt = explode(" ",$_POST['lifeline']);	//dt1
			$d = explode("-",$dt[0]);	//d1
			$tgla = $_POST['lifeline'];
			$startDate = $d[2]."-".$d[1]."-".$d[0];
			$startTime = $dt[1];
			$dt = explode(" ",$_POST['deadline']);	//tgl2
			$d = explode("-",$dt[0]);	//d1
			$tglb = $_POST['deadline'];
			$endDate = $d[2]."-".$d[1]."-".$d[0];
			$endTime = $dt[1];
			$tzOffset = "+07";
			$when = $gcal->newWhen();	//waktu baru
			$when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
			$when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
			$event->when = array($when);
			//
			$reminder = $gcal->newReminder();	//reminder
			$reminder->method = "email";
			$reminder->minutes = "1200";
			$when = $event->when[0];
			$when->reminders = array($reminder);
			//
			$uri = "https://www.google.com/calendar/feeds/agp44d2mbjilf2g0ts10c1roeg%40group.calendar.google.com/private/full";
			$gcal->insertEvent($event, $uri);   //masukkn GCal
			sendSMS($config,$tugas,$tgla,$tglb);	//kirim SMS
			addAgenda($config,$tugas,$ket,$startDate,$endDate,$where);	//masukkin agenda
			$_SESSION['addsucc'] = "Tugas berhasil ditambahkan";
		} catch (Zend_Gdata_App_Exception $e) {
			echo "Error: " . $e->getResponse();
			$_SESSION['addfail'] = "Tugas tidak berhasil ditambahkan";
		}
		header('Location: '.$config['home_url']."agenda");
	}else{
		if (isset($_SESSION['logged'])){
			view_agenda($config,array());
		}else{
			header('Location: '.$config['home_url']);
		}
	}
}
